package com.goktech.olala.client.controller.order;

import com.goktech.olala.core.service.IShopcartService;
import com.goktech.olala.server.pojo.order.Shopcart;
import com.goktech.olala.utils.RMBUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Classname ordershopController
 * @Description TODO
 * @Date 2020/12/15 22:55
 * @Created by sanming
 */
@RestController
@RequestMapping("/orderCartApi")
public class OrdershopController {

    @Autowired
    IShopcartService iShopcartService;

    /**
     * 查看购物车
     * @param request
     * @return
     */
    @RequestMapping("/queryAllShopCart.do")
    @ResponseBody
    public ModelAndView selectShopcart(HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        String customerId = (String) request.getSession().getAttribute("customerId");
        List<Shopcart> shopcarts = iShopcartService.selectCustomerId(customerId);
        mav.addObject("shopCartVos",shopcarts);
        mav.setViewName("home/shopcart");
        return mav;
    }



    /**
     * 加入购物车
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/saveShopCart.do")
    @ResponseBody
    public ModelAndView saveShopCart(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = new ModelAndView();
        String customerId = (String)request.getSession().getAttribute("customerId");
        if(StringUtils.isBlank(customerId)){
            view.setViewName("home/login");
            return view;
        }
        String realBuyPrice = request.getParameter("realBuyPrice");
        String goodsId = request.getParameter("goodsId");
        String parentGoodsId = request.getParameter("parentGoodsId");
        String goodsSn = request.getParameter("goodsSn");
        String goodsName = request.getParameter("goodsName");
        String shopPrice = request.getParameter("shopPrice");
        String marketPrice = request.getParameter("marketPrice");
        String goodsAmount = request.getParameter("goodsAmount");
        String goodsAttr = request.getParameter("goodsAttr");
        Shopcart shopcart = new Shopcart();
        shopcart.setCustomerId(customerId);
        shopcart.setGoodsId(goodsId);
        shopcart.setGoodsId(goodsId);
        shopcart.setGoodsSn(goodsSn);
        shopcart.setParentGoodsId(parentGoodsId);
        shopcart.setGoodsName(goodsName);
        int price = Integer.valueOf(RMBUtil.changeY2F(realBuyPrice));
        int amount = Integer.parseInt(goodsAmount);
        shopcart.setRealBuyPrice(price);
        shopcart.setGoodsAmount(amount);
        shopcart.setGoodsAttr(goodsAttr);
        shopcart.setMarketPrice(Integer.valueOf(RMBUtil.changeY2F(marketPrice)));
        shopcart.setShopPrice(Integer.valueOf(RMBUtil.changeY2F(shopPrice)));

        int insert = iShopcartService.insert(shopcart);
        view.addObject("result",insert);
        view.setViewName("redirect:/orderCartApi/queryAllShopCart.do");
        return view;
    }

    /**
     *删除购物车
     */
    @RequestMapping(value = "/removeShopCart.do")
    @ResponseBody
    public String removeShopCart(HttpServletRequest request, HttpServletResponse response) {
            String shopCartId = request.getParameter("shopCartId");
            Long a = new Long(shopCartId);
            String customerId = (String)request.getSession().getAttribute("customerId");
            String goodsId = request.getParameter("goodsId");

            Shopcart shopcart = new Shopcart();
            shopcart.setGoodsId(goodsId);
            shopcart.setCustomerId(customerId);
            shopcart.setShopCartId(a);

            iShopcartService.deleteShopcart(shopcart);

        return "success";
    }


}
